#include "ExGame.h"
#include "NeroGame\Nero.h"
#include "Win32\Win32.h"
#include "DirectX\DirectX11.h"

#include "FactoryDeclarations.h"
#include "HUD.h"
#include "Grid.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Ball.h"
#include "CameraController.h"

using namespace Game;

extern bool GIsRendering;

RTTI_DEFINITIONS(ExGame)
/******************************************************************************\
	Attributed :: [function]

    \brief

    \param  
    \return
\******************************************************************************/
ExGame::ExGame()
	:NGame()
{
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief

    \param  
    \return
*/
/******************************************************************************/
ExGame::~ExGame(void)
{
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief
    \param  
    \return
*/
/******************************************************************************/
void ExGame::Initialize(const std::string& scene)
{
	try
	{
		FactoryDeclarations factoryDeclare;
		NGame::Initialize(scene);

		Graphics::Camera* cam = GetPlatform()->GetCamera()->As<Graphics::Camera>();
		if(cam)
		{
			CameraController* controller = MY_NEW(Memory::HID_Game,"Cam Controller") CameraController();
			cam->AddComponent(controller);
			controller->Initialize(cam);
		}
	}
	catch(std::exception e)
	{
		OUTLOG(e.what());
	}
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief

    \param  
    \return
*/
/******************************************************************************/
void ExGame::Deinitialize()
{
	NGame::Deinitialize();
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief

    \param  
    \return
*/
/******************************************************************************/
void ExGame::Update(float deltaTime)
{
	NGame::Update(deltaTime);
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief
    \param  
    \return
*/
/******************************************************************************/
void ExGame::Render()
{
	mWorld->Render(GetPlatform()->GetDeltaTime());
	Graphics::D3DDevice* device = GetPlatform()->GetDevice()->As<Graphics::D3DDevice>();

	if(device)
	{
		static Triangle* mTriangle = NULL;
		if(!mTriangle)
		{
			mTriangle =  MY_NEW(Memory::HID_Game,"Triangle") Triangle();
			mTriangle->Initialize();
		}

	    static Graphics::Material* mTextMat = NULL;
		if(!mTextMat)
		{
			mTextMat = Graphics::MaterialResourceManager::GetInstance()->GetResource("TextureMaterial_1");
			mTextMat->Initialize();
		}

		static Graphics::Material* mColorMat = NULL;
		if(!mColorMat)
		{
			mColorMat = Graphics::MaterialResourceManager::GetInstance()->GetResource("ColorMaterial_1");
			mColorMat->Initialize();
		}

		static Ball* ball = NULL;
		if(!ball)
		{
			ball = MY_NEW(Memory::HID_Game,"Ball") Ball();
			ball->Initialize();
		}

		static Graphics::Terrain* terrain = NULL;
		if(!terrain)
		{
			terrain = MY_NEW(Memory::HID_Rendering,"Terrain") Graphics::Terrain();
			terrain->Initialize("..\\..\\assets\\textures\\heightmap01.bmp","Dirt");
		}

		static Graphics::Light* light = NULL;
		if(!light)
		{
			light = MY_NEW(Memory::HID_Rendering,"Light") Graphics::Light();
			light->SetAmbientColor(0.05f, 0.05f, 0.05f, 1.0f);
			light->SetDiffuseColor(255.0f, 255.0f, 255.0f, 255.0f);
			light->SetDirection(0.0f, 0.0f, 0.75f);
		}

		static Graphics::TerrainShader* terrainShader = NULL;
		if(!terrainShader)
		{
			terrainShader = MY_NEW(Memory::HID_Rendering, "TerrainShader") Graphics::TerrainShader();
			terrainShader->Initialize();
		}

		ball->Update(0.0f);

		/*mColorMat->Update(terrain);
		mColorMat->Render(terrain);*/

		D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
		D3DXMatrixIdentity(&worldMatrix);
		viewMatrix = Graphics::Utility::NeroToDXD(GetPlatform()->GetCamera()->GetViewMatrix());
		projectionMatrix = device->GetProjectionMatrix();
		
		terrain->Render();
		terrainShader->Render(terrain->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, light, terrain->GetTexture());

		/*mTextMat->Update(ball);
		mTextMat->Render(ball);*/
	} 
}


void ExGame::EndGame()
{
	GIsRendering = false;
	mIsGameClosing = true;
}

/******************************************************************************/
/*! Attributed :: [function]

    \brief
    \param  
    \return
*/
/******************************************************************************/
void ExGame::Notify(Engine::NEventPublisher* aEventPublisher, float wallTime)
{
}